package com.dianrui.qiyouriding.activity;

import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.event.RfreshInvest;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.SharedUtil;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 合伙人计划
 */
@SuppressWarnings("ALL")
public class PartnerPlanActivity extends BaseActivity {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.profit)
    TextView profit;
    @BindView(R.id.surplus_profit)
    TextView surplusProfit;
    @BindView(R.id.withdraw)
    TextView withdraw;


    @Override
    public int getContentViewId() {
        return R.layout.activity_partnerplan;
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        customInit(true, R.color.main_color);
        title.setText(getString(R.string.partner_plan));
        getBalanaceInfo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }


    @Override
    public void initData() {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RfreshInvest rfreshInvest) {
        getBalanaceInfo();
    }

    //获取余额（当前余额和已提现）
    private void getBalanaceInfo() {
        OkHttpRequest.getInstance().postWithMemberToken(Url.INVEST, "",  new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                profit.setText(jsonObject.optJSONObject("data").optString("profit"));//累计收益
                                surplusProfit.setText(jsonObject.optJSONObject("data").optString("surplus_profit"));//剩余收益
                                withdraw.setText(jsonObject.optJSONObject("data").optString("withdraw"));//已提现
                            } else if ("1001".equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else if ("1002".equals(jsonObject.optString("status"))) {
                                SPUtils.getInstance().clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                SharedUtil.setToken("");
                                JumpKillActivitys(LoginActivity.class);
                                finish();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });


    }


    @OnClick({R.id.back, R.id.click_argeement, R.id.click_incomewithdraw, R.id.signup, R.id.income_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            //查看合同
            case R.id.click_argeement:
                JumpActivitys(AgreeMentActivity.class);
                break;
            case R.id.click_incomewithdraw:
                JumpActivitys(IncomeDetailsListAcitivity.class);
                break;
            //立即签约
            case R.id.signup:
                JumpActivitys(SignUpActivity.class);
                break;
            //收益提现
            case R.id.income_withdraw:
                JumpActivitys(WithDrawPutForwardActivity.class);
                break;

            default:
                break;
        }
    }


}
